package cc.marcio.systems.seven.main.service;

import java.util.Calendar;
import java.util.Date;

import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.Reminder;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Reminder.Method;

public class CalendarEvent {

	private static final Integer TIME_ZONE_IN_MINUTES = -180;
	private static final Integer EVENT_DURATION_IN_MINUTES = 3;
	private static final Integer TIME_TO_ALERT_IN_MINUTES = 1;
	private static final Integer TIME_TO_START_IN_MINUTES = 5;
	
	private String title;
	private String content;
	private Date start;
	private Date end;
	private Boolean withAlert;
	
	public CalendarEvent() {
		this.title = null;
		this.content = null;
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + TIME_TO_START_IN_MINUTES);		
		this.start = new Date(now.getTimeInMillis());
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + TIME_TO_START_IN_MINUTES + EVENT_DURATION_IN_MINUTES);
		this.end = new Date(now.getTimeInMillis());
		this.withAlert = false;
	}
	
	public CalendarEvent(CalendarEventEntry entry) {
		this();
		this.title = entry.getTitle().getPlainText();
		this.content = entry.getContent().toString();
		
		for( When when : entry.getTimes() ) {
			this.start = new Date(when.getStartTime().getValue());
			this.end = new Date(when.getEndTime().getValue());
			break;
		}
		
		for( Reminder reminder : entry.getReminder() ) {
			this.withAlert = reminder != null && reminder.getMethod() != null;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public Boolean getWithAlert() {
		return withAlert;
	}

	public void setWithAlert(Boolean withAlert) {
		this.withAlert = withAlert;
	}
	
	public CalendarEventEntry getEvent() {
		CalendarEventEntry event = new CalendarEventEntry();
		event.setTitle(new PlainTextConstruct(this.getTitle()));
		event.setContent(new PlainTextConstruct(this.getContent()));
		DateTime start = new DateTime(this.getStart());
		start.setTzShift(TIME_ZONE_IN_MINUTES);
		DateTime end = new DateTime(this.getEnd());
		end.setTzShift(TIME_ZONE_IN_MINUTES);
		When when = new When();
		when.setStartTime(start);
		when.setEndTime(end);
		event.addTime(when);
		Reminder reminder = new Reminder();
		reminder.setMinutes(TIME_TO_ALERT_IN_MINUTES);
		reminder.setMethod(Method.SMS);
		event.getReminder().add(reminder);
		return event;
	}
}